package com.yh.servlet; /**
 * @Auther: Liang
 * @Date: 2024/5/16 - 05 - 16 - 10:26
 * @Description: ${PACKAGE_NAME}
 * @version: 1.0
 */

import com.yh.javabean.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "MyServlet1", value = "/MyServlet1")
public class MyServlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          //使用全局作用域来获取spring容器
        ClassPathXmlApplicationContext spring = (ClassPathXmlApplicationContext) request.getServletContext().getAttribute("spring");
       //因为知道是自己书写所以可以直接强转
        User user = (User) spring.getBean("user");
        System.out.println(user);

    }

}
